#include <log4cxx/ndc.h>
#include <log4cxx/xml/domconfigurator.h>

#include <lua.h>

#include "log.h"
#include "voice_server.h"
#include "util.h"
#include "world.h"
#include "world.h"
#include "world_voice.h"

const char LOG_NAME[] = "main";
static CWorldVoice s_worldBase;
CWorld* g_pTheWorld = &s_worldBase;

int main(int argc, char* argv[])
{
	log4cxx::xml::DOMConfigurator::configureAndWatch("log4j/voice.xml", 5000);
	log4cxx::NDC ndcMain("");
	LOG_INFO("---------------------------");
	LOG_INFO("Start voice app.");
	LOG_INFO("---------------------------");

    if(argc < 3)
    {
        LOG_ERROR("Usage: %1% cfg_file server_id" , argv[0]);
        return -1;
    }
	//second args[2] is server id for self
	uint16_t uServerId = (uint16_t)atoi(argv[2]);
	if (!Util::SetServerIdEnv(uServerId))  // for log4cxx
		return -1;

	// Must after SetServerIdEnv().
	log4cxx::xml::DOMConfigurator::configureAndWatch("log4j/voice.xml", 5000);
	LOG_INFO("-------------------------------");
	LOG_INFO(Fmt("Start voice server (ID=%1%).") % uServerId);
	LOG_INFO("-------------------------------");

	//first args[1] is configure file name
	const char* pszEtcFn = argv[1];
	CWorldVoice& worldvoice(s_worldBase);
	CVoiceServer s(uServerId, SERVER_VOICE, worldvoice);
	worldvoice.SetServer(&s);
	if (!worldvoice.init(pszEtcFn))
	{
		LOG_ERROR("World init error.");
		return -2;
	}

	uint16_t unPort = worldvoice.GetServerPort(uServerId);
	s.BlockingRun("", unPort);

	return 0;
}
